package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.event.CommentEvent;
import com.ysxsoft.wdg.ui.activity.address.AddressListActivity;
import com.ysxsoft.wdg.ui.activity.login.LoginValid;
import com.ysxsoft.wdg.util.DataCleanManager;
import com.ysxsoft.wdg.util.delayaction.SingleCall;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.widget.alertview.AlertViewFactory;
import com.ysxsoft.wdg.widget.alertview.OnItemClickListener;
import org.greenrobot.eventbus.EventBus;
import butterknife.BindView;
import butterknife.OnClick;
import static com.ysxsoft.wdg.bean.event.CommentEvent.EventStatus.MAIN_finish;

public class SettingActivity extends BaseActivity {

    public static void start(Context context){
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_cache)
    TextView tvCache;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    try {
                        tvCache.setText(DataCleanManager.getTotalCacheSize(mContext));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("设置");

        handler.sendEmptyMessage(0);

    }

    @Override
    protected void setListener() {

    }


    @OnClick({R.id.title_finish, R.id.tv_person, R.id.tv_address,
            R.id.tv_zh, R.id.tv_yjfk, R.id.tv_fwtk, R.id.tv_version,
            R.id.ll_cache, R.id.logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv_person://个人资料
                PersonActivity.start(mContext);
                break;
            case R.id.tv_address://我的收货地址
                AddressListActivity.start(mContext,0);
                break;
            case R.id.tv_zh://账号与安全
                SafeActivity.start(mContext);
                break;
            case R.id.tv_yjfk://意见与反馈
                YjfkActivity.start(mContext);
                break;
            case R.id.tv_fwtk://服务条款
                startActivity(new Intent(this,FwtkActivity.class));
                break;
            case R.id.tv_version://版本更新
                VersionActivity.start(mContext);
                break;
            case R.id.ll_cache://清除缓存
                AlertViewFactory.getInstance().getCacheAlert(mContext, new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position ==0 ){
                            DataCleanManager.clearAllCache(mContext);
                            handler.sendEmptyMessageDelayed(0,200);
                        }
                    }
                }).show();
                break;
            case R.id.logout://退出登录
                SharePrefUtils.saveUserId("");
                SharePrefUtils.clear();

                SingleCall.getInstance().addValid(new LoginValid(mContext, 0)).doCall();

                EventBus.getDefault().post(new CommentEvent(MAIN_finish));
                finish();
                break;
        }
    }
}
